package ee.emhi;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

// No warnings TextView message can be changed in string.xml - value name is setWarningsToListView

public class Hoiatused extends ListActivity {
	ArrayList<HashMap<String, String>> warning_list = new ArrayList<HashMap<String, String>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hoiatused);
		
		TabHost.icon_hoiatused.setImageResource(R.drawable.tab_hoiatused);
		
		// Checking if there is any warnings and acts according to that
		if (Splashscreen.warningsCheck()) {
			
			// Disappears the TextView that is shown when there are no warnings 
			TextView txt = (TextView) findViewById(R.id.noWarnings);
			txt.setVisibility(View.GONE);
			
			//Loading data to Listview
			displayWarnings(Splashscreen.warnings);
		}
		
	}

	public void displayWarnings(String xml){
		makeWarningDataList(xml);
		setWarningsToListView();
	}
	
	// Method goes through xml style string, maps right elements and puts them to ArrayList
	public void makeWarningDataList(String xml){
		
		Document doc = XMLfunctions.XMLfromString(xml);
		NodeList nodes = doc.getElementsByTagName("warning");
		
		for (int i = 0; i < nodes.getLength(); i++) {		
			
			HashMap<String, String> map = new HashMap<String, String>();	
			
			Element e = (Element)nodes.item(i);
			map.put("area_est", XMLfunctions.getValue(e, "area_est"));
        	map.put("content_est", XMLfunctions.getValue(e, "content_est"));
        	warning_list.add(map);			
		}	
	}
	
	// Method sets the Arraylist elements to ListView that is displayed in screen
    protected void setWarningsToListView() {
    	
    	ListAdapter adapter = new SimpleAdapter(Hoiatused.this, warning_list , R.layout.hoiatus, 
                new String[] { "area_est", "content_est"}, 
                new int[] { R.id.hoiatus_asukoht, R.id.hoiatus });

    	setListAdapter(adapter);
    }
}
